package com.dds.system.service.admin.impl;

import java.util.List;

import com.dds.system.domain.admin.AdminUserOperationChild;
import com.dds.system.mapper.admin.AdminUserOperationChildMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dds.system.mapper.admin.AdminUserOperationMapper;
import com.dds.system.domain.admin.AdminUserOperation;
import com.dds.system.service.admin.IAdminUserOperationService;

/**
 * 运营中心Service业务层处理
 * 
 * @author gaolianjie
 * @date 2021-04-14
 */
@Service
public class AdminUserOperationServiceImpl implements IAdminUserOperationService 
{
    @Autowired
    private AdminUserOperationMapper adminUserOperationMapper;

    @Autowired
    private AdminUserOperationChildMapper adminUserOperationChildMapper;

    /**
     * 查询运营中心
     * 
     * @param id 运营中心ID
     * @return 运营中心
     */
    @Override
    public AdminUserOperation selectAdminUserOperationById(Long id)
    {
        return adminUserOperationMapper.selectAdminUserOperationById(id);
    }

    /**
     * 查询运营中心列表
     * 
     * @param adminUserOperation 运营中心
     * @return 运营中心
     */
    @Override
    public List<AdminUserOperation> selectAdminUserOperationList(AdminUserOperation adminUserOperation)
    {
        return adminUserOperationMapper.selectAdminUserOperationList(adminUserOperation);
    }

    /**
     * 新增运营中心
     * 
     * @param adminUserOperation 运营中心
     * @return 结果
     */
    @Override
    public int insertAdminUserOperation(AdminUserOperation adminUserOperation)
    {
        return adminUserOperationMapper.insertAdminUserOperation(adminUserOperation);
    }

    /**
     * 修改运营中心
     * 
     * @param adminUserOperation 运营中心
     * @return 结果
     */
    @Override
    public int updateAdminUserOperation(AdminUserOperation adminUserOperation)
    {
        return adminUserOperationMapper.updateAdminUserOperation(adminUserOperation);
    }

    /**
     * 批量删除运营中心
     * 
     * @param ids 需要删除的运营中心ID
     * @return 结果
     */
    @Override
    public int deleteAdminUserOperationByIds(Long[] ids)
    {
        return adminUserOperationMapper.deleteAdminUserOperationByIds(ids);
    }

    /**
     * 删除运营中心信息
     * 
     * @param id 运营中心ID
     * @return 结果
     */
    @Override
    public int deleteAdminUserOperationById(Long id)
    {
        return adminUserOperationMapper.deleteAdminUserOperationById(id);
    }

    /**
     * 判断运营人员是否可以开店
     * @param serveUser 服务商
     * @return 结果
     */
    @Override
    public String checkOpenShopNum(String serveUser) {
        // 查看当前开店人是否在运营中心
        AdminUserOperationChild adminUserOperationChild = adminUserOperationChildMapper.selectAdminUserOperationChildByUserId(serveUser);

        if (adminUserOperationChild == null || adminUserOperationChild.getDeletedAt() != null) {
            return "开店人员不在运营中心，请先添加运营人员";
        }

        // 判断当前运营中心是否还有可开店数
        if (adminUserOperationChild.getOperationAuthorizedNum() - adminUserOperationChild.getOpenNum() < 0) {
            return "该员工已经没有可开店数";
        }

        adminUserOperationChild.setOpenNum(adminUserOperationChild.getOpenNum() + 1);
        adminUserOperationChildMapper.updateAdminUserOperationChild(adminUserOperationChild);

        return "true";
    }
}
